cc.Class({
    extends: cc.Component,

    properties: {

    },
    freshData() {
        let TBWeight = g_Res.getRes("table", "TB_SKILLSID").json;
        let sidList = [];
        let sum = 0;
        for (let sid in TBWeight) {
            sum += TBWeight[sid];
            sidList.push({
                sid: sid,
                weight: TBWeight[sid]
            });
        }
        //开始抽取,抽取三次
        let tData = {};
        for (let id in this.tbSkill) {
            let info = this.tbSkill[id];
            if (info['lv'] >= info['maxLv']) {
                //技能等级达到最高
                sidList[key]['weight'] = 0;
            }
        }
        for (let i = 0; i < 3; i++) {
            let ans = Math.random() * sum;
            let tsum = 0, key = {};
            for (key of sidList) {
                if (tsum + key['weight'] > ans) {
                    tData[key['sid']] = 1 + key['sid'] * 1;//get服务器id赋值一个客户端id
                    key['weight'] = 0;//抽取到后将权重置为0，该次技能抽取不再出现这个
                    break;
                } else {
                    tsum += key['weight'];
                }
            }
        }
        return tData;
    },
    refreshData() {
        let childList = this.skillPanel.children;
        let idx = 0;
        let resIcon = g_Res.getRes("icon", "skillIcon.plist");
        //在这里调用一个函数，使data跟新，按照权重出概率
        this.data = this.freshData();
        for (let sid in this.data) {//通过服务器ID遍历
            let { res, name, desc } = this.tbSkill[this.data[sid]];
            if (idx >= childList.length) {
                break;
            }
            let child = childList[idx++];
            child.sid = sid;//设置一个服务器id[1000,1001,1002]
            let skIcon = child.getChildByName("skIcon").getComponent(cc.Sprite);
            let skName = child.getChildByName("skName").getComponent(cc.Label);
            let skDesc = child.getChildByName("skDesc").getComponent(cc.Label);
            skIcon.spriteFrame = resIcon.getSpriteFrame(res);
            skName.string = name;
            skDesc.string = desc;

        }
    },
    onSelectSkill(sid) {
        let skID = this.data[sid];
        if (skID == 1001) {
            game.role.script.addSpeedBySkill(0.2);
            this.tbSkill[skID]['lv']++;
        } else if (skID == 1002) {
            game.role.script.addSpeedBySkill(0.4);
            this.tbSkill[skID]['lv']++;
        }
        else if (skID == 1003) {
            //增加伤害
            this.tbSkill[skID]['lv']++;
            game.role.script.addABS(25);
        } else if (skID == 1004) {
            this.tbSkill[skID]['lv']++;
            game.role.script.addABS(50);
        } else if (skID == 1005) {
            //弹道增加
            this.tbSkill[skID]['lv']++;
            game.role.script.addLaunchNum(1);
        } else if (skID == 1006) {
            //子弹带毒
            this.tbSkill[skID]['lv']++;
            game.role.script.addPoison();
        } else if (skID == 1007) {
            //火焰弹
            this.tbSkill[skID]['lv']++;
            game.role.script.addFire();
        } else if (skID == 1008) {
            //增加暴击率
            this.tbSkill[skID]['lv']++;
            game.role.script.addCritChance(20);
        } else if (skID == 1009) {
            //增加暴击倍率
            this.tbSkill[skID]['lv']++;
            game.role.script.addCritRate(50);
        } else if (skID == 1010) {
            //增加射程
            this.tbSkill[skID]['lv']++;
            game.role.script.addCircleLength(100);
        } else if (skID == 1011) {
            this.tbSkill[skID]['lv']++;
            game.role.script.addSpeed(20);
        } else if (skID == 1012) {
            //吸血
            this.tbSkill[skID]['lv']++;
            game.role.script.addLeech(1);
        }
        //这里需要将技能等级信息存储起来
        let jsonString = JSON.stringify(this.tbSkill);
        game.setSkillInfo(jsonString);
        this.node.destroy();
        game.nextRound();
    },
    //按键触发函数
    onClickButtom(event, data) {
        let node = event.target;
        g_Music.playEffict("button");
        if ([0, 1, 2].indexOf(+data) != -1) {
            this.onSelectSkill(node.sid);
        }
    },
    // onLoad () {},

    start() {
        let info = game.getSkillInfo();
        if (info != null) {
            this.tbSkill = JSON.parse(info);//将信息解序列化
        } else {
            this.tbSkill = {
                1001: {
                    res: "8001",
                    name: "攻速小",
                    desc: "生命值月底造成伤害越高",
                    lv: 0,
                    maxLv: 1000
                },
                1002: {
                    res: "8002",
                    name: "攻速大",
                    desc: "生命值月底造成伤害越高",
                    lv: 0,
                    maxLv: 1000
                },
                1003: {
                    res: "8003",
                    name: "攻击力小",
                    desc: "生命值月底造成伤害越高",
                    lv: 0,
                    maxLv: 1000
                },
                1004: {
                    res: "8004",
                    name: "攻击力大",
                    desc: "生命值月底造成伤害越高",
                    lv: 0,
                    maxLv: 1000
                },
                1005: {
                    res: "8005",
                    name: "弹道+1",
                    desc: "弹道+1",
                    lv: 0,
                    maxLv: 1000
                },
                1006: {
                    res: "8006",
                    name: "毒子弹",
                    desc: "给怪物上毒buff每秒扣10",
                    lv: 0,
                    maxLv: 1000
                },
                1007: {
                    res: "8007",
                    name: "火焰弹",
                    desc: "点燃怪物",
                    lv: 0,
                    maxLv: 1000
                },
                1008: {
                    res: "8013",
                    name: "两倍镜",
                    desc: "增加20暴击率",
                    lv: 0,
                    maxLv: 2
                },
                1009: {
                    res: "8016",
                    name: "十六倍镜",
                    desc: "增加50暴击倍率",
                    lv: 0,
                    maxLv: 2
                },
                1010: {
                    res: "8030",
                    name: "胡萝卜药水",
                    desc: "增加100攻击距离",
                    lv: 0,
                    maxLv: 3
                },
                1011: {
                    res: "8017",
                    name: "阿比达斯",
                    desc: "增加20%的移动速度",
                    lv: 0,
                    maxLv: 3
                },
                1012: {
                    res: "8029",
                    name: "吸血",
                    desc: "增加3%吸血",
                    lv: 0,
                    maxLv: 3
                }
            };
        }

        this.data = {
            1000: 1001,
            1001: 1002,
            1002: 1009,
        };
        this.skillPanel = cc.find("skillPanel", this.node);
        this.refreshData();
    },

    update(dt) { },
});
